#include "stitch.h"
#include <QRandomGenerator>

#include "config.h"
#include "coreconfig.h"
#include "task/taskmgr.h"
using namespace stitch;

Stitch::Stitch(QObject *p_parent)
    : QObject(p_parent),
    m_mainWindow(nullptr)
{
    m_instanceId = QRandomGenerator::global()->generate64();
    initThemeMgr();
    initTaskMgr();
}

void Stitch::initThemeMgr()
{
    Q_ASSERT(!m_themeMgr);
    auto &configMgr = Config::getInst();
    ThemeMgr::addSearchPath(configMgr.getAppThemeFolder());
    ThemeMgr::addSearchPath(configMgr.getUserThemeFolder());
    ThemeMgr::addSyntaxHighlightingSearchPaths(
        QStringList() << configMgr.getUserSyntaxHighlightingFolder()
                      << configMgr.getAppSyntaxHighlightingFolder());
    ThemeMgr::addWebStylesSearchPath(configMgr.getAppWebStylesFolder());
    ThemeMgr::addWebStylesSearchPath(configMgr.getUserWebStylesFolder());
    m_themeMgr = new ThemeMgr(configMgr.getCoreConfig().getTheme(), this);
}

ThemeMgr &Stitch::getThemeMgr() const
{
    return *m_themeMgr;
}

void Stitch::initTaskMgr()
{
    Q_ASSERT(!m_taskMgr);
    m_taskMgr = new TaskMgr(this);
    connect(m_taskMgr, &TaskMgr::taskOutputRequested,
            this, &Stitch::showOutputRequested);
}
TaskMgr &Stitch::getTaskMgr() const
{
return *m_taskMgr;
}

void Stitch::setMainWindow(MainWindow *p_mainWindow)
{
    Q_ASSERT(!m_mainWindow);
    m_mainWindow = p_mainWindow;
}

MainWindow *Stitch::getMainWindow() const
{
    Q_ASSERT(m_mainWindow);
    return m_mainWindow;
}

void Stitch::showTips(const QString &p_message, int p_timeoutMilliseconds)
{
    emit tipsRequested(p_message, p_timeoutMilliseconds);
}
